package com.ybkj.daijia.athe;

import com.ybkj.daijia.server.zhuan.SmallLocation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * @author zhyu
 * @date 2019-08-26 16:13:10
 */
public class ListSort {

    public static LinkedList<? extends Comparable> combineTwoSortedList(
        List<? extends Comparable> l1, List<? extends Comparable> l2) {
        if (null == l1 && null == l2) {
            return null;
        } else if (null == l1) {
            return new LinkedList<>(l2);
        } else if (null == l2) {
            return new LinkedList<>(l1);
        }
        LinkedList<Comparable> l3 = new LinkedList<>();
        int sizeA = l1.size();
        int sizeB = l2.size();
        Warpper indexA = new Warpper();
        Warpper indexB = new Warpper();

        if (0 == sizeA && 0 == sizeB) {
            return l3;
        } else if (0 == sizeA) {
            l3.addAll(l2);
            return l3;
        } else if (0 == sizeB) {
            l3.addAll(l1);
            return l3;
        }

        Comparable oa = next(l1, indexA, sizeA);
        Comparable ob = next(l2, indexB, sizeB);

        while (null != oa && null != ob) {
            if (oa.compareTo(ob) > 0) {
                l3.add(ob);
                ob = next(l2, indexB, sizeB);
            } else {
                l3.add(oa);
                oa = next(l1, indexA, sizeA);
            }
        }
        if (null == oa && null != ob) {
            l3.addAll(l2.subList(indexB.index, l2.size()));
        } else if (null == ob && null != oa) {
            l3.addAll(l1.subList(indexA.index, l1.size()));
        }
        return l3;
    }

    private static Comparable next(List<? extends Comparable> list, Warpper lastIndex, int size) {
        if (lastIndex.index < size - 1) {
            lastIndex.increament();
            return list.get(lastIndex.index);
        }
        return null;
    }

    public static void main(String[] args) {
        List<SmallLocation> l1 = new ArrayList<>();
        List<SmallLocation> l2 = new ArrayList<>();

        init(l1, l2);

        List<? extends Comparable> comparables = combineTwoSortedList(l1, l2);
        System.out.println(comparables);
    }

    private static void init(List<SmallLocation> l1, List<SmallLocation> l2) {
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            SmallLocation smallLocation = new SmallLocation();
            smallLocation.setLocDate(Long.valueOf(random.nextInt(3000)));
            l1.add(smallLocation);
        }
        for (int i = 0; i < 200; i++) {
            SmallLocation smallLocation = new SmallLocation();
            smallLocation.setLocDate(Long.valueOf(random.nextInt(3000)));
            l2.add(smallLocation);
        }
        Collections.sort(l1);
        Collections.sort(l2);
        System.out.println("l1  " + l1);
        System.out.println("----------");
        System.out.println("l2  " + l2);
        System.out.println("------------");
        List<? extends Comparable> l3 = combineTwoSortedList(l1, l2);
        System.out.println("l3  " + l3);
    }

    static class Warpper {

        private int index = -1;

        public int getIndex() {
            return index;
        }

        public void increament() {
            index++;
        }
    }
}
